#include "CCameraNode.h"

namespace cfe
{

CVectorsCamera::CVectorsCamera(void)
{
	this->setName("Camera_Default");
	m_vEye = D3DXVECTOR3(0.0f, 0.0f, -5.0f);
    m_vLook = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  
	m_vUp = D3DXVECTOR3(0.0f, 0.0f, 1.0f);    
	m_vRight = D3DXVECTOR3(-1.0f, 0.0f, 0.0f); 		
}
CVectorsCamera::~CVectorsCamera(void)
{
}
/*void CVectorsCamera::Initalize()
{
	m_vEye = D3DXVECTOR3(0.0f, 0.0f, -5.0f);
    m_vLook = D3DXVECTOR3(0.0f, 1.0f, 0.0f);  
	m_vUp = D3DXVECTOR3(0.0f, 0.0f, 1.0f);    
	m_vRight = D3DXVECTOR3(-1.0f, 0.0f, 0.0f); 		
}*/

void CVectorsCamera::Move(float fAmnt)
{
	m_vEye += m_vLook*fAmnt;
	
}

void CVectorsCamera::Strafe(float fAmnt)
{
	m_vEye += m_vRight*fAmnt;
}

/*void CVectorsCamera::Jump(float fAmnt)
{
	//fAmnt *= elapsedTime;
	m_vEye += m_vUp*fAmnt;
	//m_vEye.z += 20.0f;
}*/
void CVectorsCamera::Down(float fAmnt)
{
	m_vEye += m_vUp*fAmnt;
}

void CVectorsCamera::Rotate(float fXAmnt, float fYAmnt)
{
	D3DXMATRIX mtxRot;

    D3DXMatrixRotationAxis( &mtxRot, &m_vRight, D3DXToRadian(fYAmnt) );

	D3DXVec3TransformCoord( &m_vLook, &m_vLook, &mtxRot );
	D3DXVec3TransformCoord( &m_vUp, &m_vUp, &mtxRot );

	D3DXMatrixRotationAxis( &mtxRot, &D3DXVECTOR3(0,0,1),D3DXToRadian(fXAmnt) );
	D3DXVec3TransformCoord( &m_vLook, &m_vLook, &mtxRot );
	D3DXVec3TransformCoord( &m_vUp, &m_vUp, &mtxRot );
}

//================================================
D3DXMATRIX* CVectorsCamera::CalcViewMatrix()
{
	D3DXMatrixIdentity( &m_result);

	D3DXVec3Normalize( &m_vLook, &m_vLook );
	D3DXVec3Cross( &m_vRight, &m_vUp, &m_vLook );
	D3DXVec3Normalize( &m_vRight, &m_vRight );
	D3DXVec3Cross( &m_vUp, &m_vLook, &m_vRight );
	D3DXVec3Normalize( &m_vUp, &m_vUp );

	m_result._11 = m_vRight.x;
    m_result._12 = m_vUp.x;
    m_result._13 = m_vLook.x;
	m_result._14 = 0.0f;

	m_result._21 = m_vRight.y;
    m_result._22 = m_vUp.y;
    m_result._23 = m_vLook.y;
	m_result._24 = 0.0f;

	m_result._31 = m_vRight.z;
    m_result._32 = m_vUp.z;
    m_result._33 = m_vLook.z;
	m_result._34 = 0.0f;

	m_result._41 = -D3DXVec3Dot( &m_vEye, &m_vRight );
	m_result._42 = -D3DXVec3Dot( &m_vEye, &m_vUp );
	m_result._43 = -D3DXVec3Dot( &m_vEye, &m_vLook );
	m_result._44 =  1.0f;

    return &m_result;
}
//================================================
D3DXMATRIX* CVectorsCamera::CalcProjectionMatrix()
{
	CSettings* pSettings = CSettings::getInstance();

	D3DXMatrixPerspectiveFovLH( &m_projection, D3DXToRadian(pSettings->getFOV()), 1.3333f,
		pSettings->getZnear(), pSettings->getZfar() );
	return &m_projection;
}

};